package Synchronized;

public class DeadLockTest {
    public static void main(String[] args) {
        Left left = new Left();
        Right right = new Right();
        Thread boy = new Thread(new Boy(left,right));
        Thread girl =new Thread(new Girl(left,right));
        boy.start();
        girl.start();
    }
}
class  Left{
    String name="左边";
}
class Right{
    String name="右边";
}

class  Boy implements  Runnable{
    Left left;
    Right right;
    public Boy(Left left,Right right){
        this.left=left;
        this.right=right;
    }
    @Override
    public void run() {
        System.out.println("男孩在左边");
        synchronized (right){
            //男孩让给女孩
            try {
                right.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("男孩要在右边吃饭");
        }
        synchronized (left){
            System.out.println("男孩要在左边吃饭");
        }

    }
}
class Girl implements  Runnable{
    Left left;
    Right right;
    public Girl(Left left,Right right){
        this.left=left;
        this.right=right;
    }
    @Override
    public void run() {
        System.out.println("女孩要在右边");
        synchronized (left){
            System.out.println("女孩要在左边吃饭");
        }
        synchronized (right){
            System.out.println("女孩要在右边吃饭");
            //女孩吃完饭后,h唤醒等待男孩的线程
            right.notify();
        }

    }
}